package net.wanho.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sun.org.apache.xpath.internal.operations.Or;
import net.wanho.hospital.constant.OrderConstant;
import net.wanho.hospital.po.Orders;
import net.wanho.hospital.mapper.OrdersMapper;
import net.wanho.hospital.service.OrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 汤小洋
 * @since 2023-12-27
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

   /**
    *   按查询分页查询
    */
    @Override
    public IPage<Orders> findPage(int pageNum, int pageSize,Orders orders) {
        QueryWrapper<Orders> wrapper = null;
        IPage<Orders> page = new Page<>(pageNum, pageSize);
        page = this.page(page, wrapper);
        return page;
    }

    @Override
    public Map<String, Object> findAllOrders(int pageNumber, int size, String query) {
        Page<Orders> page = new Page<>(pageNumber, size);

        LambdaQueryWrapper<Orders> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(!ObjectUtils.isEmpty(query), Orders::getPId, query)
                .orderByAsc(Orders::getOId);

        this.page(page, wrapper);

        Map<String,Object> map = new HashMap<>();
        map.put("orders", page.getRecords());
        map.put("total", page.getTotal());

        return map;
    }

    @Override
    public void addOrder(Orders order, String arId) {
        /**
         * 判断患者在当前时间段是否有他未就诊的挂号单
         */

        order.setOState(OrderConstant.STATE_UNFINISHED);
        order.setOPriceState(OrderConstant.PRICE_STATE_NO);
        order.setOStart(order.getOStart().substring(0,22));

        this.save(order);
    }

    @Override
    public List<Orders> findByPid(Integer pId) {
        return this.baseMapper.selectByPid(pId);
    }

    @Override
    public List<Orders> findOrderByNull(Integer dId, String oStart) {
        return this.baseMapper.selectOrdersByNull(dId, oStart);
    }

    @Override
    public void updatePrice(String oId) {
        Orders orders = this.getById(oId);
        orders.setOPriceState(OrderConstant.PRICE_STATE_OK);
        orders.setOEnd(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        this.updateById(orders);
    }

    @Override
    public Map<String, Object> findOrderByDid(int pageNum, int pageSize, Integer dId, String pId) {
        Page<Orders> page = new Page<>(pageNum, pageSize);

        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.eq("d.d_id", dId)
                .eq(!ObjectUtils.isEmpty(pId), "p.p_id", pId);

        IPage<Orders> p = this.baseMapper.selectOrderByDid(page, wrapper);


        Map<String,Object> map = new HashMap<>();
        map.put("orders", page.getRecords());
        map.put("total", page.getTotal());

        return map;
    }

    @Override
    public Map<String, Object> findOrderFinish(int pageNum, int pageSize, Integer dId, String pId) {

        Page<Orders> page = new Page<>(pageNum, pageSize);

        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.eq("d.d_id", dId)
                .eq("o.o_state", OrderConstant.STATE_FINISHED)
                .eq(!ObjectUtils.isEmpty(pId), "p.p_id", pId);

        IPage<Orders> p = this.baseMapper.selectOrderByDid(page, wrapper);


        Map<String,Object> map = new HashMap<>();
        map.put("orders", page.getRecords());
        map.put("total", page.getTotal());

        return map;
    }

}
